package com.wlyuan.open.scanner.domain.customer;

import com.wlyuan.open.scanner.dao.entity.CustomerDO;
import com.wlyuan.open.scanner.utils.JsonUtils;
import com.wlyuan.open.scanner.utils.mappings.BaseMapping;
import com.wlyuan.open.scanner.utils.mappings.MappingInvalid;
import org.mapstruct.*;
import org.mapstruct.factory.Mappers;

import java.util.List;

/**
 * @author lijing
 * @description
 */
public class CustomerConvert {

    private static final Convert CONVERT = Mappers.getMapper(Convert.class);

    @Mapper(uses = {MappingInvalid.class})
    public interface Convert {
        @Mappings({
                @Mapping(target = "id", source = "customerId"),
                @Mapping(target = "name", source = "customerName"),
                @Mapping(target = "companyAbbr", source = "customerShortname"),
                @Mapping(target = "abbr", source = "customerShortname"),
                @Mapping(target = "englishName", source = "customerEnname"),
                @Mapping(target = "englishAbbr", source = "customerShortenname"),
                @Mapping(target = "autoBalance", source = "customerAutobalance"),
                @Mapping(target = "logo", source = "customerLogo"),
                @Mapping(target = "fax", source = "customerFax"),
                @Mapping(target = "bankName", source = "customerBank"),
                @Mapping(target = "zipcode", source = "customerZip"),
                @Mapping(target = "address", source = "customerAddress"),
                @Mapping(target = "bankAccount", source = "customerBankaccount"),
                @Mapping(target = "corporation", source = "customerMaster"),
                @Mapping(target = "businessLicenseUrl", source = "customerLicense"),
                @Mapping(target = "contactEmail", source = "customerMail"),
                @Mapping(target = "contactMobile", source = "customerPhone"),
                @Mapping(target = "contactName", source = "customerContact"),
                @Mapping(target = "hots", source = "customerHotcount"),
                @Mapping(target = "industry", source = "customerIndustry"),
                @Mapping(target = "profile", source = "customerDescription"),
                @Mapping(target = "remarks", source = "customerComments"),
                @Mapping(target = "type", source = "customerUnderline"),
                @Mapping(target = "tenantId", source = "customerOwnercompany"),

                @Mapping(target = "sourceKey", source = "customerId"),
                @Mapping(target = "createTime", source = "customerInserttime"),
                @Mapping(target = "updateTime", source = "customerUpdatetime"),
                @Mapping(target = "createBy", source = "customerUpdater"),
                @Mapping(target = "updateBy", source = "customerCreator"),
        })
        @BaseMapping
        Customer convert(CustomerDO customerDO);

        @AfterMapping
        default void after(@MappingTarget Customer target, CustomerDO source) {
            target.setSourceId(source.getCustomerId() + "#" + source.getCustomerOwnercompany());
            target.setRaw(JsonUtils.withNullJson(source));
        }

        List<Customer> convert(List<CustomerDO> list);

    }

    public static List<Customer> convert(List<CustomerDO> creation) {
        return CONVERT.convert(creation);
    }

    public static Customer convert(CustomerDO creation) {
        return CONVERT.convert(creation);
    }
}
